﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DiyBook.Models;

namespace DiyBook.Controllers
{
    public class BookController : Controller
    {
        //
        // GET: /Book/
        BooksDB db = new BooksDB();
        [Authorize(Roles = "Admin")]
        public ActionResult Index()
        {
            var book = from m in db.Books
                           select m;
            return View(book.ToList());
        }
        [Authorize(Roles = "Admin")]
        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        public ActionResult AddBook(Book newBook)
        {
            
            if (ModelState.IsValid)
            {   Book b = new Book();
                b.ISBN = Request.Form["ISBN"];
                b.bookName = Request.Form["bookName"];
                b.publishNum = Request.Form["publishNum"];
                b.contents = Request.Form["contents"];
                b.publisher = Request.Form["publisher"];
                b.authors = Request.Form["authors"];
                b.b_Count = int.Parse(Request.Form["b_Count"]);
                b.count = b.b_Count;
                b.classType1 = Request.Form["classType1"];
                db.Books.Add(b);
                db.SaveChanges();
                //借书方法
                //int id = 0;
                //Book b1 = db.Books.Find(id);
                //b1.count=b1.count-1;
                //UpdateModel(b1);
                //db.SaveChanges();
                return RedirectToAction("Index");                
            }
            else
                return View(newBook);
        }
        [Authorize(Roles = "Admin")]
        public ActionResult Details(int id)
        {
            Book book = db.Books.Find(id);
            if (book == null)
                return RedirectToAction("Index");
            return View("Details", book);
        }
        [Authorize(Roles = "Admin")]
        public ActionResult Edit(int id)
        {
            Book book = db.Books.Find(id);
            if (book == null)
                return RedirectToAction("Index");

            return View(book);
        }

        [HttpPost]
        public ActionResult Edit(Book model)
        {
            try
            {
                var book = db.Books.Find(model.bookId);
                UpdateModel(book);
                db.SaveChanges();
                return RedirectToAction("Details", new { id = model.bookId });
            }
            catch (Exception)
            {
                ModelState.AddModelError("", "修改失败，请查看详细错误信息");
            }

            return View(model);
        }
        [Authorize(Roles = "Admin")]
        public ActionResult Delete(int id)
        {
            Book book = db.Books.Find(id);
            if (book == null)
                return RedirectToAction("Index");
            return View(book);
        }

        [HttpPost]
        public RedirectToRouteResult Delete(int id, FormCollection collection)
        {
            var book = db.Books.Find(id);
            db.Books.Remove(book);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
    }
}